package com.aimao.blog.service.impl;

import com.aimao.blog.constant.ServerResponse;
import com.aimao.blog.dao.AdminMapper;
import com.aimao.blog.dao.ArticleMapper;
import com.aimao.blog.dao.CommentMapper;
import com.aimao.blog.dao.SubjectMapper;
import com.aimao.blog.pojo.Admin;
import com.aimao.blog.pojo.Comment;
import com.aimao.blog.service.AccountAdminService;
import com.aimao.blog.util.CustomMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.CacheNamespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author PuaChen
 * 管理员操作类
 * @Create 2018-07-02 17:44
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class AccountAdminServiceImpl implements AccountAdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public Admin getAdminBean(String accountId) {
        return adminMapper.selectByPrimaryKey(accountId);
    }

    @Override
    public ServerResponse<Admin> updateAdmin(String accountId, String name, String head) {
        Admin admin = new Admin();
        admin.setId(accountId);
        admin.setName(name);
        admin.setMainimg(head);
        int row = adminMapper.updateByPrimaryKeySelective(admin);
        if (row <= 0) {
            return ServerResponse.createFailure("更新失败");
        }
        return ServerResponse.createSuccessAndData(getAdminBean(accountId));
    }

    @Override
    public ServerResponse<Admin> updateAdminPassword(String accountId, String oldPwd, String newPwd) {
        if (StringUtils.isNotBlank(accountId)) {
            Admin oldAd = getAdminBean(accountId);
            if (oldAd == null) {
                return ServerResponse.createFailure("用户不存在");
            }
            if (!oldAd.getPassword().equals(oldPwd)) {
                return ServerResponse.createFailure("原密码输入错误");
            }
            Admin admin = new Admin();
            admin.setId(accountId);
            admin.setPassword(newPwd);
            int row = adminMapper.updateByPrimaryKeySelective(admin);
            if (row <= 0) {
                return ServerResponse.createFailure("更新失败");
            } else {
                return ServerResponse.createSuccessAndData(getAdminBean(accountId));
            }
        }
        return ServerResponse.createFailure("参数错误");
    }

    @Override
    public ServerResponse<Admin> login(String accountId, String pwd) {
        Admin adminBean = getAdminBean(accountId);
        if (adminBean == null) {
            return ServerResponse.createFailure("用户名不存在");
        }
        if (!adminBean.getPassword().equals(pwd)) {
            return ServerResponse.createFailure("密码输入错误");
        }
        return ServerResponse.createSuccessAndData(adminBean);
    }

    @Override
    public void setLastLogoutTime(Admin admin) {
        if (admin == null) {
            return;
        }
        Admin up = new Admin();
        up.setId(admin.getId());
        up.setLasttime(new Date());
        adminMapper.updateByPrimaryKeySelective(up);
    }

    @Override
    public ServerResponse<Map<String, Object>> getStatisticsData() {
        int articleCount = articleMapper.selectCountAll();
        int subjectCount = subjectMapper.selectCountAll();
        List<Comment> notHandle = commentMapper.selectNotHandleAll();
        int notHandleCount = notHandle.size();

        return ServerResponse.createSuccessAndData(CustomMap.create()
                .add("articleCount", articleCount)
                .add("subjectCount", subjectCount)
                .add("notHandleCount", notHandleCount)
                .add("notHandle", notHandle)
        );
    }

    @Override
    public ServerResponse setCommentStatus(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return ServerResponse.createFailure();
        }

        int row = commentMapper.updateCommentStatusForDone(ids);
        if (row <= 0) {
            return ServerResponse.createFailure("更新失败");
        }
        return ServerResponse.createSuccess();
    }
}
